package com.htthorizon.auth.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import com.htthorizon.common.core.domain.R;
import com.htthorizon.common.core.utils.StringUtils;
import com.htthorizon.system.api.RemoteUserService;
import com.htthorizon.system.api.domain.SysUser;
import com.sgcc.isc.core.orm.identity.Department;
import com.sgcc.isc.core.orm.identity.User;
import com.sgcc.isc.service.adapter.factory.AdapterFactory;
import com.sgcc.isc.service.adapter.helper.IIdentityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author Brilliant
 */
@RestController
@Slf4j
@CrossOrigin
public class AddUserController {

    @Autowired
    private RemoteUserService userService;

    @GetMapping("/getUserByName")
    public R<List<Map<String,String>>> getUserByName(@RequestParam(value = "userName",required = false) String userName) {
        try{
            if (StringUtils.isEmpty(userName)){
                return R.fail("不能为空");
            }
            IIdentityService service = AdapterFactory.getIdentityService();

            List<User> usersByNames = service.getUsersByName(userName);
            List<Map<String,String>> list = new ArrayList<>();
            for (User user:usersByNames){
                Map<String, String> map = new HashMap<>();
                map.put("name",user.getName());
                map.put("userName",user.getUserName());
                list.add(map);
            }
            return R.ok(list);

        }catch (Exception e){

            log.error("错误{}",e);

            return R.fail();

        }
    }

    @GetMapping("/getDeptById")
    public R<Department> getDeptById(@RequestParam("id") String id){
        try{
            IIdentityService service = AdapterFactory.getIdentityService();
            Department department = service.getDepartmentById(id);
            return R.ok(department);
        }catch (Exception e){
            log.info("错误{}",e);
            return R.fail();
        }
    }
    @GetMapping("/getObjectsByUserId")
    public R getObjectByUserId(@RequestParam("userId") String userId,@RequestParam("sysId") String sysId){
        IIdentityService service = AdapterFactory.getIdentityService();
        try {
            HashMap<String, List> objects = service.getObjectsByUserId(userId, sysId);
            return R.ok(objects);
        } catch (Exception e) {
            log.info("错误{}",e);
            return R.fail();
        }
    }


}
